package com.ssy.lingxi.member.merchant.serviceimpl.feign;

import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.member.merchant.entity.MemberRelationDO;
import com.ssy.lingxi.member.merchant.model.constant.CheckStatusEnum;
import com.ssy.lingxi.member.merchant.model.constant.MemberInnerStatusEnum;
import com.ssy.lingxi.member.merchant.api.constant.MemberRelationTypeEnum;
import com.ssy.lingxi.member.merchant.model.constant.PlatformInnerStatusEnum;
import com.ssy.lingxi.member.merchant.service.feign.IMessageFeignService;
import com.ssy.lingxi.message.api.constant.MessageTemplateCode;
import com.ssy.lingxi.message.api.feign.SystemMessageControllerFeign;
import com.ssy.lingxi.message.api.vo.request.SystemMessageRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 调用消息服务Feign接口
 * @author yzc
 * @date 2021/6/23 15:47
 **/
@Service
public class MessageFeignServiceImpl implements IMessageFeignService {
    private static final Logger logger = LoggerFactory.getLogger(MessageFeignServiceImpl.class);

    @Resource
    private SystemMessageControllerFeign systemMessageControllerFeign;

    @Override
    public SystemMessageRequest buildSystemMessage(Long memberId, Long roleId, String messageCode, List<String> params) {
        SystemMessageRequest request = new SystemMessageRequest();
        request.setMemberId(memberId);
        request.setRoleId(roleId);
        request.setParams(params);
        request.setMessageNotice(messageCode);
        return request;
    }

    @Override
    public SystemMessageRequest buildUserSystemMessage(Long memberId, Long roleId, Long userId, String messageCode, List<String> params) {
        SystemMessageRequest request = new SystemMessageRequest();
        request.setMemberId(memberId);
        request.setRoleId(roleId);
        request.setUserId(userId);
        request.setParams(params);
        request.setMessageNotice(messageCode);
        return request;
    }

    @Async
    @Override
    public void sendSystemMessage(Long memberId, Long roleId, String messageCode, List<String> params) {
        SystemMessageRequest request = this.buildSystemMessage(memberId, roleId, messageCode, params);
        Wrapper<Void> wrapper = systemMessageControllerFeign.sendSystemMessage(request);
        if (wrapper.getCode() != ResponseCode.SUCCESS.getCode()) {
            logger.error(wrapper.getMessage());
        }
    }

    @Async
    @Override
    public void batchSendSystemMessage(List<SystemMessageRequest> request) {
        if (CollectionUtils.isEmpty(request)) {
            return;
        }
        Wrapper<Void> wrapper = systemMessageControllerFeign.batchSendSystemMessage(request);
        if (wrapper.getCode() != ResponseCode.SUCCESS.getCode()) {
            logger.error(wrapper.getMessage());
        }
    }

    @Async
    @Override
    public void sendUserSystemMessage(Long memberId, Long roleId, Long userId, String messageCode, List<String> params) {
        SystemMessageRequest request = this.buildUserSystemMessage(memberId, roleId, userId, messageCode, params);
        Wrapper<Void> wrapper = systemMessageControllerFeign.sendSystemMessage(request);
        if (wrapper.getCode() != ResponseCode.SUCCESS.getCode()) {
            logger.error(wrapper.getMessage());
        }
    }

    /**
     * 平台会员审核、会员入库审核时，发送消息
     *
     * @param relation 会员关系
     * @param memberId 消息接收方会员Id
     * @param roleId 消息接收方会员角色Id
     */
    @Async
    @Override
    public void sendMemberValidateMessage(MemberRelationDO relation, Long memberId, Long roleId) {
        String messageCode;
        List<String> parameters = Stream.of(relation.getSubRoleName(), relation.getSubMember().getName()).collect(Collectors.toList());
        if (relation.getRelType().equals(MemberRelationTypeEnum.PLATFORM.getCode())) {
            PlatformInnerStatusEnum innerStatusEnum = PlatformInnerStatusEnum.parse(relation.getInnerStatus());
            switch (innerStatusEnum) {
                case TO_BE_COMMIT:
                    messageCode = MessageTemplateCode.member_platform_submit;
                    break;
                case TO_BE_VERIFY_STEP1:
                    messageCode = MessageTemplateCode.member_platform_audit_step_1;
                    break;
                case TO_BE_VERIFY_STEP2:
                    messageCode = MessageTemplateCode.member_platform_audit_step_2;
                    break;
                case COMMIT_NOT_PASSED:
                case VERIFY_STEP1_NOT_PASSED:
                case VERIFY_STEP2_NOT_PASSED:
                case TO_CONFIRM:
                    messageCode = MessageTemplateCode.member_platform_audit_confirm;
                    break;
                case VERIFY_PASSED:
                    messageCode = MessageTemplateCode.platform_member_audit;
                    parameters = Stream.of(relation.getSubRoleName(), CheckStatusEnum.AGREE.getMessage()).collect(Collectors.toList());
                    break;
                case VERIFY_NOT_PASSED:
                    messageCode = MessageTemplateCode.platform_member_audit;
                    parameters = Stream.of(relation.getSubRoleName(), CheckStatusEnum.REFUSE.getMessage()).collect(Collectors.toList());
                    break;
                default:
                    messageCode = "";
            }
        } else {
            MemberInnerStatusEnum innerStatusEnum = MemberInnerStatusEnum.parse(relation.getInnerStatus());
            switch (innerStatusEnum) {
                case TO_VERIFY_DEPOSITORY_DETAIL:
                    messageCode = MessageTemplateCode.mc_storage_info_audit;
                    break;
                case TO_VERIFY_DEPOSITORY_QUALIFICATION:
                    messageCode = MessageTemplateCode.mc_storage_qualification_audit;
                    break;
                case TO_INSPECT_DEPOSITORY:
                    messageCode = MessageTemplateCode.mc_storage_inspect;
                    break;
                case TO_CLASSIFY_DEPOSITORY:
                    messageCode = MessageTemplateCode.mc_storage_classify;
                    break;
                case TO_DEPOSIT_GRADE_ONE:
                    messageCode = MessageTemplateCode.mc_storage_audit_step_1;
                    break;
                case TO_DEPOSIT_GRADE_TWO:
                    messageCode = MessageTemplateCode.mc_storage_audit_step_2;
                    break;
                case DEPOSITORY_DETAIL_NOT_PASSED:
                case DEPOSITORY_QUALIFICATION_NOT_PASSED:
                case DEPOSITORY_INSPECTION_NOT_PASSED:
                case DEPOSITORY_CLASSIFICATION_NOT_PASSED:
                case DEPOSITORY_GRADE_ONE_NOT_PASSED:
                case DEPOSITORY_GRADE_TWO_NOT_PASSED:
                case TO_CONFIRM_DEPOSITORY:
                    messageCode = MessageTemplateCode.mc_storage_confirm;
                    break;
                case VERIFY_PASSED:
                    messageCode = MessageTemplateCode.up_member_audit;
                    parameters = Stream.of(relation.getMember().getName(), relation.getSubRoleName(), CheckStatusEnum.AGREE.getMessage()).collect(Collectors.toList());
                    break;
                case VERIFY_NOT_PASSED:
                    messageCode = MessageTemplateCode.up_member_audit;
                    parameters = Stream.of(relation.getMember().getName(), relation.getSubRoleName(), CheckStatusEnum.REFUSE.getMessage()).collect(Collectors.toList());
                    break;
                case TO_MODIFY_GRADE_ONE:
                    messageCode = MessageTemplateCode.mc_modification_step_1;
                    break;
                case TO_MODIFY_GRADE_TWO:
                    messageCode = MessageTemplateCode.mc_modification_step_2;
                    break;
                case MODIFY_GRADE_ONE_NOT_PASSED:
                case MODIFY_GRADE_TWO_NOT_PASSED:
                case TO_CONFIRM_MODIFY:
                    messageCode = MessageTemplateCode.mc_modification_confirm;
                    break;
                default:
                    messageCode = "";
                    break;
            }
        }

        if (!StringUtils.hasLength(messageCode)) {
            return;
        }

        SystemMessageRequest request = new SystemMessageRequest();
        request.setMemberId(memberId);
        request.setRoleId(roleId);
        request.setMessageNotice(messageCode);
        request.setParams(parameters);
        Wrapper<?> wrapper = systemMessageControllerFeign.sendSystemMessage(request);
        if (wrapper.getCode() != ResponseCode.SUCCESS.getCode()) {
            logger.error("发送会员审核消息错误：" + wrapper.getMessage());
        }
    }

    /**
     * 会员入库审核时，发送消息
     *
     * @param relation 会员关系
     */
    @Override
    public void sendMemberValidateMessage(MemberRelationDO relation) {
        sendMemberValidateMessage(relation, relation.getSubMemberId(), relation.getSubRoleId());
    }
}
